using System.Web.Mvc;
using AddressBook.Models;
using ManagedUIFlow.Infrastructure.Commands.FlowActionHelpers;

namespace AddressBook.Controllers
{
    public class SearchController : FlowController
    {

        private readonly PersonRepository _personRepository;

        public SearchController()
        {
            _personRepository = new PersonRepository();
        }

        //
        // GET: /Search/

        public ActionResult Validate(string firstName, string lastName)
        {
            if (!string.IsNullOrEmpty(firstName) || !string.IsNullOrEmpty(lastName))
                return State("done");

            ViewData.Model = new SearchViewModel { ErrorMessage = "Please provide criteria e.g. A, R etc..." };

            return State("error");
        }

        public ActionResult Process(string firstName, string lastName)
        {

            var persons = _personRepository.FindBy(firstName, lastName);          

            if(persons.Count == 0)
            {
                ViewData.Model = new SearchViewModel { ErrorMessage = "No results found." };
                return State("failure");
            }

            ViewData.Model = new SearchViewModel();

            FlowData["persons"] = persons;

            return State("success");
        }       
    }
}